<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2021/4/26
 * Time: 10:53
 */

namespace app\api\controller\v1;

use app\common\model\Article as ArticleModel;
use app\common\model\Cate;

class Article extends BaseController
{
    public function article()
    {
        $type = input('type/d');
        $page = input('page/d', 1);
        $id = input('cate_id/d');
        $cate = Cate::where(['type' => $type, 'status' => 1])->field('id,name,pid')->select()->toArray();
        //  取出所有分类的id
        $cate_id = array_column($cate, 'id');
        $cate = getTree($cate);
        if (!empty($id)) {
            $where = ['cate_id' => $id];
            $cate_this = Cate::find($id);
        } else {
            if (!$cate) {
                return errMsg(400, '请传入参数');
            }
            $where[] = ['cate_id', 'in', $cate_id];
            if (input('type') == 3) {
                $cate_this = ['id' => 0, 'name' => '代码知识'];
            } elseif (input('type') == 4) {
                $cate_this = ['id' => 0, 'name' => '问答知识'];
            } else {
                $cate_this = ['id' => 0, 'name' => '技术社区'];
            }
        }
        if (input('type') == 3) {
            $mbx = ['id' => 0, 'name' => '代码知识'];
        } elseif (input('type') == 4) {
            $mbx = ['id' => 0, 'name' => '问答知识'];
        } else {
            $mbx = ['id' => 0, 'name' => '技术社区'];
        }
        $res = ArticleModel::where($where)
            ->where('status', 1)
            ->field('id,title,content,zan_num,comment_num,collect_num,image,created_at')
            ->order('sort asc')
            ->limit(($page - 1) * 5, 5)
            ->select()
            ->toArray();
        $maxpage = ArticleModel::where($where)
            ->where('status', 1)
            ->field('id')
            ->count();
        $maxpage = ceil($maxpage / 5);
        if ($res) {
            $res = changeImage($res, 'image');
            $res = changeTime($res, 'created_at');
            $res = delHtml($res, 'content');
        }
        $all = ArticleModel::where($where)
            ->where('status', 1)
            ->field('id,title,content,zan_num,comment_num,collect_num,image,created_at')
            ->order('sort asc')
            ->limit(($page - 1) * 5, 6)
            ->select()
            ->toArray();
        if ($all) {
            foreach ($all as &$v) {
                if (!empty($v['image']))
                    $v['image'] = env('app.yuming') . $v['image'];
                if (!empty($v['file_path']))
                    $v['file_path'] = env('app.yuming') . $v['file_path'];
            }
        }
        $data = ['cate' => $cate,
            'cate_this' => $cate_this,
            'mbx' => $mbx,
            'res' => $res,
            'maxpage' => $maxpage,
            'all'=>$all
        ];
        return sucessMsg(200, '获取成功', $data);
    }

    public function content()
    {
        $id = input('id/d');
        if (!$id) {
            return errMsg(400, '请传入id');
        }
        $data = ArticleModel::find($id);
        $type = Cate::where('id', $data->cate_id)->value('type');
        $data->image = env('app.yuming') . $data->image;
        //  上一篇
        $up = ArticleModel::where('id', '<', $id)
            ->where('status', 1)
            ->where('cate_id', $data['cate_id'])
            ->field('id,title')
            ->order('id asc')
            ->find();
        //下一篇
        $next = ArticleModel::where('id', '>', $id)
            ->where('status', 1)
            ->where('cate_id', $data['cate_id'])
            ->field('id,title')
            ->order('id asc')
            ->find();

        $data = ['data' => $data, 'type' => $type, 'up' => $up, 'next' => $next];
        return sucessMsg(200, '获取成功', $data);
    }
}